package com.comleader.operator.validate;

import static cn.hutool.core.text.CharSequenceUtil.*;
import static com.comleader.operator.common.constant.Constant.*;
import static com.comleader.operator.common.constant.DictConstant.*;
import static com.comleader.operator.validate.CommonValidator.*;

import com.comleader.operator.pojo.entity.PersonalUserEntity;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.IdcardUtil;

/**
 * @Dscription 个人用户校验器
 * @Author 连新景
 * @Date 2024/08/05 11:05
 */
public class PersonalUserValidator {

    /*
     * @Description 校验格式异常
     * @Auhtor 连新景
     * @Date 2024/08/05 11:08
     * @param entity 
     * @return boolean
     */
    public static boolean isFormatException(PersonalUserEntity entity) {
        String customerType = entity.getCustomerType();
        // 必填项判断
        if (hasBlank(entity.getUserNumber(), entity.getNumberDate(), entity.getNumberStatus(), entity.getUserBusinessType(), customerType,
            entity.getChannelType())) {
            return true;
        }
        if (isDateFormatException(entity.getNumberDate(), DatePattern.PURE_DATE_FORMATTER)) {
            return true;
        }
        // 校验字段的值是否在字典配置中已配置
        if (BooleanUtil.or(isDictIncorrect(CUSTOMER_TYPE, entity.getCustomerType()), isDictIncorrect(USER_BUSINESS_TYPE, entity.getUserBusinessType()),
            isDictIncorrect(PERSONAL_CHANNEL_TYPE, entity.getChannelType()),
            MOBILE_PHONE.equals(entity.getUserBusinessType()) && isDictIncorrect(BUSINESS_USA_STATUS, entity.getBusinessUsaStatus()),
            // 号码状态分为在网和不在网，这两个字段中都没有时才认为格式异常
            isDictIncorrect(PERSONAL_NET_STATE, entity.getNumberStatus()) && isDictIncorrect(PERSONAL_NOT_NET_STATE, entity.getNumberStatus()))) {
            return true;
        }

        // 个人用户
        if (PERSON_USER.equals(customerType)) {
            // 校验机主
            if (hasBlank(entity.getOwnerName(), entity.getOwnerIdType(), entity.getOwnerIdNumber(), entity.getOwnerIdAddress(),
                entity.getOwnerMailingAddress(), entity.getOwnerInstallAddress())) {
                return true;
            }
            // 不是居民身份证，或者身份证号不对，无法获取年龄，也没必要校验代办人了
            if (!RESIDENT_IDCARD.equals(entity.getOwnerIdType()) || !IdcardUtil.isValidCard(entity.getOwnerIdNumber())) {
                return false;
            }
            // 小于16岁大于120岁校验代办人
            int age = IdcardUtil.getAgeByIdCard(entity.getOwnerIdNumber());
            if (age < 16 || age > 120) {
                if (hasBlank(entity.getAgentName(), entity.getAgentIdType(), entity.getAgentIdNumber(), entity.getAgentIdAddress(),
                    entity.getAgentMailingAddress())) {
                    return true;
                }
            }
            // 单位用户
        } else if (COMPANY_USER.equals(customerType)) {
            // 为员工办理移动电话卡时，校验机主信息
            if (MOBILE_PHONE.equals(entity.getUserBusinessType())) {
                if (hasBlank(entity.getOwnerName(), entity.getOwnerIdType(), entity.getOwnerIdNumber(), entity.getOwnerIdAddress(),
                    entity.getOwnerMailingAddress(), entity.getOwnerInstallAddress())) {
                    return true;
                }
            }
            // 校验代办人，校验单位信息，有一个为true，则返回true
            if (BooleanUtil.or(isAgentFormatException(entity), isCompanyFormatException(entity))) {
                return true;
            }
            // 行业用户
        } else if (INDUSTRY_USER.equals(customerType)) {
            // 校验代办人，校验单位信息，有一个为true，则返回true
            if (BooleanUtil.or(isAgentFormatException(entity), isCompanyFormatException(entity))) {
                return true;
            }
            // 校验责任人信息
            if (hasBlank(entity.getPicName(), entity.getPicIdType(), entity.getPicIdNumber(), entity.getPicIdAddress(),
                entity.getPicMailingAddress())) {
                return true;
            }
        }
        // 网点信息
        if (PHYSICAL_CHANNEL.equals(entity.getChannelType())) {
            if (isBlank(entity.getOpenerNumber())) {
                if (hasBlank(entity.getNetworkNumber(), entity.getNetworkName(), entity.getNetworkAddress(), entity.getNetworkCounty(),
                    entity.getNetworkType())) {
                    return true;
                }
            } else {
                if (hasBlank(entity.getNetworkAddress(), entity.getNetworkType())) {
                    return true;
                }
            }
        }
        return false;
    }

    /*
     * @Description 校验代办人格式异常
     * @Auhtor 连新景
     * @Date 2024/08/06 9:55
     * @param entity 
     * @return boolean
     */
    private static boolean isAgentFormatException(PersonalUserEntity entity) {
        return hasBlank(entity.getAgentName(), entity.getAgentIdType(), entity.getAgentIdNumber(), entity.getAgentIdAddress(),
            entity.getAgentMailingAddress());
    }

    /*
     * @Description 校验单位格式异常
     * @Auhtor 连新景
     * @Date 2024/08/06 9:55
     * @param entity 
     * @return boolean
     */
    private static boolean isCompanyFormatException(PersonalUserEntity entity) {
        return hasBlank(entity.getCompanyName(), entity.getCompanyIdNumber(), entity.getCompanyIdType(), entity.getCompanyIdAddress(),
            entity.getCompanyMailingAddress(), entity.getCompanyInstallAddress());
    }

    /**
     * @Description 校验个人用户-经办人未登记
     * @Auhtor 连新景
     * @Date 2024/08/05 11:16
     * @param entity
     * @return boolean
     */
    public static boolean isPersonalAgentUnregister(PersonalUserEntity entity) {
        // 不是居民身份证，或者身份证号不对，无法获取年龄，也没必要校验代办人了
        if (!RESIDENT_IDCARD.equals(entity.getOwnerIdType()) || !IdcardUtil.isValidCard(entity.getOwnerIdNumber())) {
            return false;
        }
        int age = IdcardUtil.getAgeByIdCard(entity.getOwnerIdNumber());
        if (age < 16 || age > 120) {
            if (isAllBlank(entity.getAgentName(), entity.getAgentIdType(), entity.getAgentIdNumber(), entity.getAgentIdAddress(),
                entity.getAgentMailingAddress())) {
                return true;
            }
        }
        return false;
    }

    /**
     * @Description 校验使用人未登记
     * @Auhtor 连新景
     * @Date 2024/08/05 16:07
     * @param entity
     * @return boolean
     */
    public static boolean isUserUnregister(PersonalUserEntity entity) {
        return isAllBlank(entity.getOwnerName(), entity.getOwnerIdType(), entity.getOwnerIdNumber(), entity.getOwnerIdAddress(),
            entity.getOwnerMailingAddress(), entity.getOwnerInstallAddress());
    }

    /**
     * @Description 校验经办人未登记
     * @Auhtor 连新景
     * @Date 2024/08/05 11:16
     * @param entity
     * @return boolean
     */
    public static boolean isAgentUnregister(PersonalUserEntity entity) {
        return isAllBlank(entity.getAgentName(), entity.getAgentIdType(), entity.getAgentIdNumber(), entity.getAgentIdAddress(),
            entity.getAgentMailingAddress());
    }

    /*
     * @Description 校验单位信息未登记
     * @Auhtor 连新景
     * @Date 2024/08/05 16:11
     * @param entity
     * @return boolean
     */
    public static boolean isCompanyUnregister(PersonalUserEntity entity) {
        return isAllBlank(entity.getCompanyName(), entity.getCompanyIdType(), entity.getCompanyIdNumber(), entity.getCompanyIdAddress(),
            entity.getCompanyMailingAddress(), entity.getCompanyInstallAddress());
    }

    /**
     * @Description 校验行业用户-责任人未登记
     * @Auhtor 连新景
     * @Date 2024/08/05 11:16
     * @param entity
     * @return boolean
     */
    public static boolean isIndustryUserPicUnregister(PersonalUserEntity entity) {
        return isAllBlank(entity.getPicName(), entity.getPicIdType(), entity.getPicIdNumber(), entity.getPicIdAddress(), entity.getPicMailingAddress());

    }

    /**
     * @Description 校验使用人不合规
     * @Auhtor 连新景
     * @Date 2024/08/05 14:36
     * @param entity
     * @return boolean
     */
    public static boolean isUserNoncompliant(PersonalUserEntity entity) {
        return BooleanUtil
            .or(isNotBlank(entity.getOwnerName()) && !Validator.isChineseName(entity.getOwnerName()),
                isNotBlank(entity.getOwnerIdType()) && isDictIncorrect(PERSONAL_IDCARD_TYPE, entity.getOwnerIdType()),
                RESIDENT_IDCARD.equals(entity.getOwnerIdType()) && isNotBlank(entity.getOwnerIdNumber())
                    && !IdcardUtil.isValidCard(entity.getOwnerIdNumber()),
                isAddressNoncompliant(entity.getOwnerMailingAddress(), entity.getOwnerInstallAddress()));
    }

    /**
     * @Description 校验个人用户经办人不合规
     * @Auhtor 连新景
     * @Date 2024/08/06 10:19
     * @param entity
     * @return boolean
     */
    public static boolean isPersonalAgentNoncompliant(PersonalUserEntity entity) {
        // 不是居民身份证，或者身份证号不对，无法获取年龄，也没必要校验代办人了
        if (!RESIDENT_IDCARD.equals(entity.getOwnerIdType()) || !IdcardUtil.isValidCard(entity.getOwnerIdNumber())) {
            return false;
        }
        int age = IdcardUtil.getAgeByIdCard(entity.getOwnerIdNumber());
        if (age < 16 || age > 120) {
            return isAgentNoncompliant(entity);
        }
        return false;
    }

    /**
     * @Description 校验经办人不合规
     * @Auhtor 连新景
     * @Date 2024/08/05 14:36
     * @param entity
     * @return boolean
     */
    public static boolean isAgentNoncompliant(PersonalUserEntity entity) {
        return BooleanUtil.or(isNotBlank(entity.getAgentName()) && !Validator.isChineseName(entity.getAgentName()),
            isNotBlank(entity.getAgentIdType()) && isDictIncorrect(PERSONAL_IDCARD_TYPE, entity.getAgentIdType()),
            RESIDENT_IDCARD.equals(entity.getAgentIdType()) && isNotBlank(entity.getAgentIdNumber())
                && !IdcardUtil.isValidCard(entity.getAgentIdNumber()),
            isAddressNoncompliant(entity.getAgentMailingAddress()));
    }

    /*
     * @Description 校验单位信息不合规
     * @Auhtor 连新景
     * @Date 2024/08/06 10:42
     * @param entity
     * @param customerType
     * @param userBusinessType
     * @return boolean
     */
    public static boolean isCompanyNoncompliant(PersonalUserEntity entity) {
        return isAddressNoncompliant(entity.getCompanyIdAddress(), entity.getCompanyMailingAddress(), entity.getCompanyInstallAddress());
    }

    /**
     * @Description 校验行业用户-责任人不合规
     * @Auhtor 连新景
     * @Date 2024/08/05 11:16
     * @param entity
     * @return boolean
     */
    public static boolean isIndustryUserPicNoncompliant(PersonalUserEntity entity) {
        return BooleanUtil.or(isNotBlank(entity.getPicName()) && !Validator.isChineseName(entity.getPicName()),
            isNotBlank(entity.getPicIdType()) && isDictIncorrect(PERSONAL_IDCARD_TYPE, entity.getPicIdType()),
            RESIDENT_IDCARD.equals(entity.getPicIdType()) && isNotBlank(entity.getPicIdNumber()) && !IdcardUtil.isValidCard(entity.getPicIdNumber()),
            isAddressNoncompliant(entity.getPicMailingAddress()));

    }

}
